//
//  RPAppShareView.m
//  ShowYork
//
//  Created by hxh on 2020/11/24.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "RPAppShareView.h"

@interface RPAppShareView ()
// 分享平台
@property (nonatomic,assign)XHShareViewPlatformType platformType;
/// 自定义分享对象
@property (nonatomic,strong)NSObject *itemModel;
/// 绑定的vc
@property (nonatomic,strong)UIViewController *shareVc;

@property (nonatomic,strong)UIView *containView;

@property (nonatomic,strong)UIButton *cancelBtn;

@property (nonatomic,strong)UILabel *titleLb;

@end

@implementation RPAppShareView

+ (void)showCompleteblock:(XHShareViewBlock)block{
    
    RPAppShareView *shareView = [RPAppShareView new];
    
    shareView.frame = [UIScreen mainScreen].bounds;
    
    [shareView showCompleteblock:block];
    
    [[UIApplication sharedApplication].keyWindow addSubview:shareView];
        
}

- (void)showCompleteblock:(XHShareViewBlock)block{
  
    self.block = block;
    
    self.backgroundColor = RGBA(0, 0, 0, 0.5);
    
    self.containView = [[UIView alloc]init];
    [self addSubview:self.containView];
    self.containView.backgroundColor = [UIColor whiteColor];
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self);
        make.height.mas_equalTo(211);
    }];
    
    self.titleLb = [[UILabel alloc]init];
    self.titleLb.text = @"分享到";
    self.titleLb.textColor = RGBA(51, 51, 51, 1);
    self.titleLb.font = font(14);
    [self.containView addSubview:self.titleLb];
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.containView);
        make.top.equalTo(self.containView).offset(16);
    }];
    
    NSArray *titleArray = @[@"微信好友",@"朋友圈",@"QQ",@"QQ空间",@"微博"];
    NSArray *imageArray = @[@"common_share_wechatSession_tag",@"common_share_wechatTimeLine_tag",@"common_share_qq_tag",@"common_share_qqzone_tag",@"common_share_weibo_tag"];
    CGFloat width = 36;
    CGFloat margin = 30;
    CGFloat itemMargin = (kScreenW - margin *2 - width * 5)/4;
    
    UIButton *lastBtn = nil;
    for (int i = 0; i < titleArray.count; i++) {
        UIButton *btn = [[UIButton alloc]init];
        btn.tag = i;
        [btn setImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateHighlighted];
        [btn addTarget:self action:@selector(shareMethod:) forControlEvents:UIControlEventTouchUpInside];
        [self.containView addSubview:btn];
        
        UILabel *lb = [[UILabel alloc]init];
        lb.text = titleArray[i];
        lb.textColor = C102;
        lb.font = font(12);
        [self.containView addSubview:lb];
        if (i == 0) {
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(width, width));
                make.top.equalTo(self.titleLb.mas_bottom).offset(20);
                make.left.equalTo(self.containView).offset(margin);
            }];
            
        }else{
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(width, width));
                make.left.equalTo(lastBtn.mas_right).offset(itemMargin);
                make.centerY.equalTo(lastBtn);
            }];
        }
        [lb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(btn);
            make.top.equalTo(btn.mas_bottom).offset(8);
        }];
        lastBtn = btn;
    }
    self.cancelBtn = [[UIButton alloc]init];
    [self.cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelBtn setTitleColor:RGBA(51, 51, 51, 1) forState:UIControlStateNormal];
    self.cancelBtn.titleLabel.font = font(18);
    self.cancelBtn.backgroundColor = RGBA(248, 248, 248, 1);
    self.cancelBtn.layer.cornerRadius = 22;
    self.cancelBtn.layer.masksToBounds = YES;
    [self.cancelBtn addTarget:self action:@selector(cancelMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.containView addSubview:self.cancelBtn];
    
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lastBtn.mas_bottom).offset(45);
        make.height.mas_equalTo(44);
        make.left.equalTo(self.containView).offset(16);
        make.right.equalTo(self.containView).offset(-16);
    }];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelMethod)];
    [self addGestureRecognizer:tap];
}
- (void)cancelMethod{
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self removeFromSuperview];
}
- (void)shareMethod:(UIButton *)btn{
    self.block(btn.tag);
    [self cancelMethod];
}
- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.containView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(15, 15)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.containView.bounds;
    maskLayer.path = maskPath.CGPath;
    self.containView.layer.mask = maskLayer;
}

@end
